const app = getApp();
import {
    baseURL
} from "../../utils/util"

Page({
    data: {
        comment: "",
        comments: [],
        isPrized: false,
        user: {}
    },
    // 页面加载
    onLoad: function (options) {
        var id = options.id;
        this.GetArticleCommentsById(id);

        this.setData({
            item: wx.getStorageSync('currentArticle')
        })
        this.setData({
            id: id,
            user: app.globalData.user
        });
    },

    returnHome() {
        console.log("return")
        wx.switchTab({
            url: "/pages/index/index"
        })
    },
    GetArticleCommentsById(id) {
        wx.request({
            url: 'https://mock.apipost.net/mock/4077a55ac0e0000/?apipost_id=a1e9ed6758015',
            method: 'GET',
            success: (res) => {
                console.log(res.data)
                this.setData({
                    comments: res.data
                })
            }
        })
    },
    GetArticleById(id) {
        wx.request({
            //   url: baseURL+'articles/'+id,
            url: "https://mock.apipost.net/mock/4077a55ac0e0000/?apipost_id=a18b8ba358008",
            method: 'GET',
            success: (res) => {
                console.log(res.data)
                this.setData({
                    item: res.data
                })
            }
        })
    },

    inputFrame(e) {
        this.setData({
            comment: e.detail.value
        })
    },

    publishComment: function () {
        if (this.data.comment === '') {
            wx.showToast({
                title: '输入为空！',
                icon: 'error'
            })
        } else {
            wx.request({
                url: 'http://127.0.0.1:8080/addComment',
                method: 'GET',
                data: {
                    articleId: this.data.item.id,
                    comment: this.data.comment,
                    userId: app.globalData.user.id
                },
                success: (res) => {
                    console.log(res.data)
                    this.setData({
                        comment: "",
                        comments: res.data
                    })
                }
            })
        }
    },

    prizeAction: function () {
        wx.request({
            url: 'http://127.0.0.1:8080/like',
            method: 'GET',
            data: {
                articleId: this.data.item.id,
                userId: app.globalData.user.id
            },
            success: (res) => {
                console.log(res.data)
                console.log(app.globalData.user.likedArticle)
                if (this.data.isPrized) {
                    app.globalData.user.likedArticle.splice(app.globalData.user.likedArticle.indexOf(this.data.item.id), 1)
                } else {
                    app.globalData.user.likedArticle.unshift(this.data.item.id)
                }
                console.log(app.globalData.user.likedArticle)
                this.setData({
                    "item.likes": this.data.item.likes + (this.data.isPrized ? -1 : 1),
                    "isPrized": !this.data.isPrized,
                })
            }
        })
    }
});